package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Channel;

/**
 * A data access object (DAO) providing persistence and search support for
 * Channel entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fick.hibernate.dao.Channel
 * @author MyEclipse Persistence Tools
 */

public class ChannelDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ChannelDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String FATHER_ID = "fatherId";
	public static final String SHOW_ORDER = "showOrder";
	public static final String URL = "url";
	public static final String IS_SHOW = "isShow";

	public void attachClean(Channel instance) {
		log.debug("attaching clean Channel instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Channel instance) {
		log.debug("attaching dirty Channel instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Channel instances");
		try {
			String queryString = "from Channel order by showOrder";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByExample(Channel instance) {
		log.debug("finding Channel instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Channel").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByFatherId(Object fatherId) {
		return findByProperty(FATHER_ID, fatherId);
	}

	public Channel findById(java.lang.Integer id) {
		log.debug("getting Channel instance with id: " + id);
		try {
			Channel instance = (Channel) getSession().get("com.fick.hibernate.model.Channel", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByIsShow(Object isShow) {
		return findByProperty(IS_SHOW, isShow);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Channel instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Channel as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByShowOrder(Object showOrder) {
		return findByProperty(SHOW_ORDER, showOrder);
	}

	public List findByUrl(Object url) {
		return findByProperty(URL, url);
	}

	public Channel findFather(int id) {
		Channel channel = findById(id);
		int i = channel.getFatherId();
		if (i != 1) {
			findFather(i);
		}
		return channel;
	}

	public Channel merge(Channel detachedInstance) {
		log.debug("merging Channel instance");
		try {
			Channel result = (Channel) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}